package cn.com.common.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;


public class PropertiesUtil {
	/**
	 * get class properties value
	 * @param filePath
	 * @param key
	 * @return propertes value
	 */
	public static String getCPvalue(String filePath, String key) {
		Properties props = new Properties();
		InputStream in = null;
		try {
			in = PropertiesUtil.class.getClassLoader().getResourceAsStream(
					filePath);
			props.load(in);
			String value = props.getProperty(key);
			in.close();
			return value;
		} catch (Exception e) {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e1) {

				}
			}
			return "";
		}
	}

	/**
	 * get class properties map collection
	 * @param filePath
	 * @return map collection
	 */
	public static Map<String, String> getCPvalues(String filePath) {
		Map<String, String> map = new HashMap<String, String>();
		Properties props = new Properties();
		InputStream reader = null;
		try {
			reader = PropertiesUtil.class.getClassLoader().getResourceAsStream(
					filePath);
			props.load(reader);
			reader.close();
			Iterator<String> it = props.stringPropertyNames().iterator();
			while (it.hasNext()) {
				String key = it.next();
				map.put(key, props.getProperty(key));
			}
		} catch (Exception e) {
			try {
				reader.close();
			} catch (IOException e1) {
				// ///
			}
		}
		return map;
	}

	/**
	 *  set class properties value
	 * @param filePath
	 * @param key
	 * @param value
	 * @return  properties value
	 */
	public static String setCPvalue(String filePath, String key, String value) {
		filePath = PropertiesUtil.class.getResource("/" + filePath).toString();
		filePath = filePath.substring(6);
		Properties prop = new Properties();
		try {
			File file = new File(filePath);
			if (!file.exists())
				file.createNewFile();
			InputStream fis = new FileInputStream(file);
			prop.load(fis);
			fis.close();
			OutputStream fos = new FileOutputStream(filePath);
			prop.setProperty(key, value);
			prop.store(fos, "Update '" + key + "' value");
			fos.close();
			return "ok";
		} catch (IOException e) {
			// System.err.println("");
			return "false";
		}
	}

	/**
	 * get servert properties value
	 * @param request
	 * @param filePath
	 * @param key
	 * @return peoperties value
	 */
	public static String getSevertPvalue(HttpServletRequest request,
			String filePath, String key) {
		InputStreamReader reader = null;
		try {
			String realPath = request.getSession().getServletContext()
					.getRealPath(filePath);// getServletContext()相当于http://localhost/demo05
			reader = new InputStreamReader(new FileInputStream(realPath),
					"utf-8");
			Properties props = new Properties();
			props.load(reader);
			String resultValue = props.getProperty(key);
			reader.close();
			return resultValue;
		} catch (Exception e) {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
			return "";
		}

	}

	/**
	 * get properties map collection
	 * @param request
	 * @param filePath
	 * @return map collection
	 */
	public static Map<String, String> getSevertPvalues(
			HttpServletRequest request, String filePath) {
		Map<String, String> map = new HashMap<String, String>();
		InputStreamReader reader = null;
		try {
			String realPath = request.getSession().getServletContext()
					.getRealPath(filePath);//
			reader = new InputStreamReader(new FileInputStream(realPath),
					"utf-8");
			Properties props = new Properties();
			props.load(reader);
			reader.close();
			Iterator<String> it = props.stringPropertyNames().iterator();
			while (it.hasNext()) {
				String key = it.next();
				map.put(key, props.getProperty(key));
			}
			return map;
		} catch (Exception e) {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
			return map;
		}

	}

	/**
	 * set properties value
	 * @param request
	 * @param filePath
	 * @param skey
	 * @param svalue
	 * @return set state
	 * @throws IOException
	 */
	public static String setSevertPvalue(HttpServletRequest request,
			String filePath, String skey, String svalue) throws IOException {
		InputStreamReader reader = null;
		try {
			String realPath = request.getSession().getServletContext()
					.getRealPath(filePath);//

			// System.out.println(realPath);
			reader = new InputStreamReader(new FileInputStream(realPath),
					"utf-8");
			Properties props = new Properties();
			props.load(reader);
			reader.close();
			FileOutputStream oFile = new FileOutputStream(filePath, true);
			props.setProperty(skey, svalue);
			props.store(oFile, "");
			oFile.close();
			return "ok";
		} catch (Exception e) {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
			return "false";
		}
	}
}
